<?php
namespace app\manage\logic;

/**
 * api接口的分类逻辑层
 * @author adv30 <adv30@126.com>
 */ 
class ApiCate extends BaseLogic
{
    // 设置数据表（不含前缀）
    protected $name = 'api_cate';
    
    // 获取分类树
    public function getCateTree ()
    {
        $list = $this->db()
            ->where('status', '1')
            ->order(['level'=>'ASC','parent_id'=>'ASC', 'order'=>'ASC'])
            ->select();
        $tree = [];
        if( is_array($list) ){
            foreach ($list as $k => $v) {
                if( 1 == $v['level'] ){
                    $tree[$v['id']] = $v;
                    unset( $list[$k] );
                }
            }
            foreach ($list as $k => $v) {
                if( 2 == $v['level']){
                    $tree[$v['parent_id']]['children'][$v['id']] = $v;
                    unset( $list[$k] );
                }
            }
        }
        return $tree;
    }
    // 增加一条分类记录
    public function addCate ($data)
    {
        return $this->model('app\manage\model\ApiCate')->save($data);
    }
    // 禁用一条接口分类
    public function removeCate ($data)
    {
        $apiCate['id'] = $data['id'];
        $apiCate['status'] = 0;
        return $this->model('app\manage\model\ApiCate')::update($apiCate);
    }
}
